<template>
  <el-dialog v-model="dialogVisible" title="请输入管理员密钥" width="500">
    <el-form>
      <el-form-item label="管理员密钥：">
        <el-input
              v-model="opCode"
              type="password"
              show-password
              placeholder="请输入管理员密钥"
              maxlength="18"
          ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleConfirm">提交</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import * as httpService from '@/services/apply.service'
import { useUserStore } from '@/stores/useStore'

// 接收 visible 作为 prop 控制对话框显示与隐藏

const opCode = ref('')
const dialogVisible = ref(false)


// 处理确认提交
const handleConfirm = async () => {
  if (opCode.value) {
    const res =  await httpService.checkOpCode({
      opCode: opCode.value
    })
    if (res.data == "ok") {
      useUserStore().setOpCode(opCode.value)
      dialogVisible.value = false
    } else {
      ElMessage.error('管理员密钥错误')
      useUserStore().setOpCode("")
    }
  } else {
    ElMessage.error('管理员密钥不能为空')
  }
}

// 处理取消操作
const handleCancel = () => {
  dialogVisible.value = false
  opCode.value = ""
}

// 显示对话框的方法
const show = () => {
  dialogVisible.value = true
}

// 暴露方法给父组件
defineExpose({
  show
})
</script>    